package com.hunliji.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunliji.common.exception.WarnException;
import com.hunliji.common.response.PageData;
import com.hunliji.monitor.dao.mapper.mt.BusinessChangeOrderMapper;
import com.hunliji.monitor.dao.model.dataobject.BusinessChangeOrderDO;
import com.hunliji.monitor.dao.model.dataobject.GroupStatisticsDO;
import com.hunliji.monitor.dao.model.param.QueryOrderAffirmCountGroupDataParam;
import com.hunliji.monitor.service.api.BusinessChangeOrderService;
import com.hunliji.monitor.service.api.model.bo.BusinessChangeOrderBO;
import com.hunliji.monitor.service.api.model.param.QueryOrderAffirmCountGroupParam;
import com.hunliji.monitor.service.api.model.param.QueryTableChangeOrderParam;
import com.hunliji.monitor.share.component.ComponentLocator;
import com.hunliji.monitor.share.utils.PageDataUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 变更单
 * @author wuchen
 */
@Service
public class BusinessChangeOrderServiceImpl implements BusinessChangeOrderService {
    @Resource
    private BusinessChangeOrderMapper businessChangeOrderMapper;
    @Override
    public BusinessChangeOrderBO add(BusinessChangeOrderBO businessChangeOrderBO) {
        BusinessChangeOrderDO businessChangeOrderDO = ComponentLocator.getConverter().convert(businessChangeOrderBO,BusinessChangeOrderDO.class);
        businessChangeOrderMapper.insert(businessChangeOrderDO);
        businessChangeOrderBO.setId(businessChangeOrderDO.getId());
        return businessChangeOrderBO;
    }

    @Override
    public void update(BusinessChangeOrderBO businessChangeOrderBO) {
        if(Objects.isNull(businessChangeOrderBO.getId())){
            throw new WarnException("id 不能为空");
        }
        BusinessChangeOrderDO businessChangeOrderDO = businessChangeOrderMapper.selectById(businessChangeOrderBO.getId());
        if(Objects.isNull(businessChangeOrderDO)){
            throw new WarnException("无法查询到变更单记录");
        }
        businessChangeOrderDO = ComponentLocator.getConverter().convert(businessChangeOrderBO,BusinessChangeOrderDO.class);
        businessChangeOrderMapper.updateById(businessChangeOrderDO);
    }

    @Override
    public BusinessChangeOrderBO queryById(Long id) {
        if(Objects.isNull(id)){
            throw new WarnException("id 不能为空");
        }
        BusinessChangeOrderDO businessChangeOrderDO = businessChangeOrderMapper.selectById(id);
        if(Objects.isNull(businessChangeOrderDO)){
            throw new WarnException("无法查询到变更单记录");
        }

        return ComponentLocator.getConverter().convert(businessChangeOrderDO,BusinessChangeOrderBO.class);
    }

    @Override
    public List<BusinessChangeOrderBO> query(QueryTableChangeOrderParam queryTableChangeOrderParam) {
        LambdaQueryWrapper<BusinessChangeOrderDO> queryWrapper = new LambdaQueryWrapper<BusinessChangeOrderDO>();
        queryWrapper.eq(BusinessChangeOrderDO::getDeleted,Boolean.FALSE);
        if(Objects.nonNull(queryTableChangeOrderParam.getTableRecordId())){
            queryWrapper.eq(BusinessChangeOrderDO::getLinkId, queryTableChangeOrderParam.getTableRecordId());
        }
        if(!CollectionUtils.isEmpty(queryTableChangeOrderParam.getTableRecordIds())){
            queryWrapper.in(BusinessChangeOrderDO::getLinkId, queryTableChangeOrderParam.getTableRecordIds());
        }
        if(Objects.nonNull(queryTableChangeOrderParam.getOrderStatus())){
            queryWrapper.eq(BusinessChangeOrderDO::getOrderStatus, queryTableChangeOrderParam.getOrderStatus());
        }
        if(!CollectionUtils.isEmpty(queryTableChangeOrderParam.getIds())){
            queryWrapper.in(BusinessChangeOrderDO::getId,queryTableChangeOrderParam.getIds());
        }
        List<BusinessChangeOrderDO> businessChangeOrderDOList = businessChangeOrderMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(businessChangeOrderDOList)){
            return new ArrayList<>();
        }
        return ComponentLocator.getConverter().convertList(businessChangeOrderDOList,BusinessChangeOrderBO.class);
    }

    @Override
    public PageData<BusinessChangeOrderBO> queryPage(QueryTableChangeOrderParam queryTableChangeOrderParam) {
        LambdaQueryWrapper<BusinessChangeOrderDO> queryWrapper = new LambdaQueryWrapper<BusinessChangeOrderDO>();
        queryWrapper.eq(BusinessChangeOrderDO::getDeleted,Boolean.FALSE);
        if(Objects.nonNull(queryTableChangeOrderParam.getTableRecordId())){
            queryWrapper.eq(BusinessChangeOrderDO::getLinkId, queryTableChangeOrderParam.getTableRecordId());
        }
        if(!CollectionUtils.isEmpty(queryTableChangeOrderParam.getTableRecordIds())){
            queryWrapper.in(BusinessChangeOrderDO::getLinkId, queryTableChangeOrderParam.getTableRecordIds());
        }
        if(Objects.nonNull(queryTableChangeOrderParam.getOrderStatus())){
            queryWrapper.eq(BusinessChangeOrderDO::getOrderStatus, queryTableChangeOrderParam.getOrderStatus());
        }
        if(Objects.nonNull(queryTableChangeOrderParam.getUserId())){
            queryWrapper.eq(BusinessChangeOrderDO::getUserId,queryTableChangeOrderParam.getUserId());
        }
        PageHelper.startPage(queryTableChangeOrderParam.getPageNum(), queryTableChangeOrderParam.getPageSize());
        List<BusinessChangeOrderDO> businessChangeOrderDOList = businessChangeOrderMapper.selectList(queryWrapper);
        PageInfo<BusinessChangeOrderDO> pageInfo = new PageInfo<>(businessChangeOrderDOList);
        if(CollectionUtils.isEmpty(businessChangeOrderDOList)){
            return PageDataUtil.getEmptyData();
        }
        List<BusinessChangeOrderBO> businessChangeOrderBOList = ComponentLocator.getConverter().convertList(businessChangeOrderDOList,BusinessChangeOrderBO.class);
        return PageDataUtil.getPageData((int)pageInfo.getTotal(),businessChangeOrderBOList);
    }




}
